﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webnew.Systems
{
    public partial class View_Company : WebPageBase.PageBase
    {
        private List<Model.Sys_CompanyInfo> CompanyList = null;
        private List<Model.Sys_DeptInfo> DeptList = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //显示所有导航和按钮
                CompanyList = new BLL.Sys_Company().GetList();
                DeptList = new BLL.Sys_Dept().GetList();
                CreateTree();
            }
        }

        #region  生成多级单位树
        private void CreateTree()
        {
            foreach (Model.Sys_CompanyInfo model in CompanyList.Where(p => p.ParentID == Guid.Empty.ToString()))
            {
                TreeNode ParentNode = new TreeNode(model.compname, model.Id.ToString());
                CreateDeptTree(ParentNode, ParentNode.Value); //生成多级单位下的部门树
                ParentNode.SelectAction = TreeNodeSelectAction.Expand;
                TreeView1.Nodes.Add(ParentNode);
                GetChildNodes(ParentNode);
                
            }
        }

        private void GetChildNodes(TreeNode ParentNode)
        {
            foreach (Model.Sys_CompanyInfo model in CompanyList.Where(p => p.ParentID == ParentNode.Value))
            {
                TreeNode ChildNode = new TreeNode(model.compname, model.Id.ToString());
                ParentNode.SelectAction = TreeNodeSelectAction.Expand;
                CreateDeptTree(ChildNode, ChildNode.Value); //生成多级单位下的部门树
                if (CompanyList.Exists(p => p.ParentID == model.Id)) GetChildNodes(ChildNode);
                ParentNode.ChildNodes.Add(ChildNode);
               
            }
        }
        #endregion


        #region 生成多级单位下的部门树
        private void CreateDeptTree(TreeNode ParentNode,string strCompId)
        {
            foreach (Model.Sys_DeptInfo model in DeptList.Where(p => p.Compid == strCompId))
            {
                TreeNode ChlidNode = new TreeNode(model.deptname, model.Id.ToString());
                ChlidNode.SelectAction = TreeNodeSelectAction.Expand;
                ParentNode.ChildNodes.Add(ChlidNode);
                GetChildDeptNodes(ChlidNode);
            }
        }

        private void GetChildDeptNodes(TreeNode ParentNode)
        {
            foreach (Model.Sys_DeptInfo model in DeptList.Where(p => p.ParentID == ParentNode.Value))
            {
                TreeNode ChildNode = new TreeNode(model.deptname, model.Id.ToString());
                ParentNode.SelectAction = TreeNodeSelectAction.Expand;
                if (DeptList.Exists(p => p.ParentID == model.Id)) GetChildDeptNodes(ChildNode);
                ParentNode.ChildNodes.Add(ChildNode);
            }
        }
        #endregion
    }
}
